﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Drawing;
using MVCClubSite.IDAL;
using MVCClubSite.Model;
using MVCClubSite.BLL;
using MVCClubSite.Web.ViewData;
using System.IO;
using MVCClubSite.Helpers;

namespace MVCClubSite.Web.Controllers
{
    public class AlbumsController : BaseController
    {
        AlbumsService _albumsService;
        ImagesService _imagesService;

        public AlbumsController()
            : this(null, null)
        {
        }

        public AlbumsController(AlbumsService albumsService, ImagesService imagesService)
        {
            _albumsService = albumsService ?? new AlbumsService();
            _imagesService = imagesService ?? new ImagesService();
        }

        public ActionResult List()
        {
            AlbumListViewData data = new AlbumListViewData();
            data.Albums = _albumsService.GetAllAlbums();
            return View("List", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult New()
        {
            AlbumViewData data = new AlbumViewData();
            data.Album = new Album();
            return View("New", data);
        }

        [AcceptVerbs("POST")]
        public ActionResult Create(FormCollection form)
        {
            Album newAlbum = new Album();
            this.UpdateModel(newAlbum, new[] {"Title", "Description", "IsPrivate" });

            if (String.IsNullOrEmpty(form["Title"]))
            {
                ModelState.AddModelError("title", "You must specify a title.");
            }

            if (ViewData.ModelState.IsValid)
            {
                _albumsService.Save(newAlbum);

                return RedirectToAction("List");
            }
            AlbumViewData data = new AlbumViewData();
            data.Album = newAlbum;
            return View("New", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult Edit(string id, string selectedImage)
        {
            int albumID = int.Parse(id);
            AlbumViewData data = new AlbumViewData();
            data.Album = _albumsService.GetAlbumByID(albumID);
            data.Images = _imagesService.GetImagesByAlbumID(albumID);

            if (data.Images.Count != 0 && selectedImage == "")
                data.SelectedImage = data.Images[0].ID;
            else if (selectedImage != "")
                data.SelectedImage = int.Parse(selectedImage);

            return View("Edit", data);
        }

        [AcceptVerbs("POST")]
        public ActionResult Update(FormCollection form, int id)
        {
            Album existingAlbum = _albumsService.GetAlbumByID(id);
            this.UpdateModel(existingAlbum,new [] { "Title", "IsPrivate" });

            if (String.IsNullOrEmpty(form["Title"]))
            {
                ModelState.AddModelError("title", "You must specify a title.");
            }

            if (ViewData.ModelState.IsValid)
            {
                _albumsService.Save(existingAlbum);

                return RedirectToAction("Details", "Albums", new { id = id });
            }

            AlbumViewData data = new AlbumViewData();
            data.Album = existingAlbum;
            data.Images = _imagesService.GetImagesByAlbumID(existingAlbum.ID);
            return View("Edit", data);
        }

        [AcceptVerbs("POST")]
        public ActionResult Delete(int id)
        {
            _albumsService.Delete(id);
            return RedirectToAction("List", "Albums");
        }

        [AcceptVerbs("POST")]
        public ActionResult UploadImage(int id, string imageTitle)
        {
            HttpPostedFileBase posted = Request.Files["file"];
            if (posted != null && posted.ContentLength > 0)
            {
                Model.Image newImage = new MVCClubSite.Model.Image();
                newImage.AlbumID = id;
                newImage.Description = imageTitle;
                newImage.OriginalImage = new byte[posted.ContentLength];
                System.IO.Stream myStream = posted.InputStream;
                myStream.Read(newImage.OriginalImage, 0, posted.ContentLength);
                _imagesService.Save(newImage);
            }

            return RedirectToAction("Edit", "Albums", new { id = id.ToString() });
        }

        public ActionResult ImageSelected(string id)
        {
            int albumID = int.Parse(id);
            AlbumViewData data = new AlbumViewData();
            data.Album = _albumsService.GetAlbumByID(albumID);
            data.Images = _imagesService.GetImagesByAlbumID(albumID);

            return View("Edit", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult Details(string id, string selectedImage)
        {
            int albumID = int.Parse(id);
            AlbumViewData data = new AlbumViewData();
            data.Album = _albumsService.GetAlbumByID(albumID);
            data.Images = _imagesService.GetImagesByAlbumID(albumID);

            if (data.Images.Count != 0 && selectedImage == "")
                data.SelectedImage = data.Images[0].ID;
            else if (selectedImage != "")
                data.SelectedImage = int.Parse(selectedImage);

            return View("Details", data);
        }
    }
}
